package net.yla.smartbus.module.ba.duty.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.DepartmentDTO;
import com.github.pagehelper.PageHelper;
import net.yla.smartbus.core.api.RequestCode;
import com.vip.vjtools.vjkit.collection.ListUtil;
import net.yla.smartbus.core.base.BaseService;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.exception.AppException;
import net.yla.smartbus.module.ba.dept.entity.DeptDO;
import net.yla.smartbus.module.ba.dept.entity.GroupChildrenDO;
import net.yla.smartbus.module.ba.dept.form.DeptForm;
import net.yla.smartbus.module.ba.dept.query.DeptEmpQuery;
import net.yla.smartbus.module.ba.duty.dto.DeptDTO;
import net.yla.smartbus.module.ba.duty.dto.DutyDto;
import net.yla.smartbus.module.ba.duty.entity.DutyDO;
import net.yla.smartbus.module.ba.duty.form.DutyForm;
import net.yla.smartbus.module.ba.duty.mapper.DutyMapper;
import net.yla.smartbus.module.ba.duty.query.DutyQuery;
import net.yla.smartbus.module.ba.duty.service.DutyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: Yee
 * @date: 2021/6/17 14:48
 */
@Service
public class DutyServiceImpl extends BaseService implements DutyService{

    @Autowired
    private DutyMapper dutyMapper;

    @Override
    public PageBean<DutyDto> listDuties(DutyQuery query) {
        if (query.getGroupId()!=null) {
            List<Integer> childrenDOS = this.listGroupId(query.getGroupId());
            query.setGroupIdList(childrenDOS);
        }
        PageHelper.startPage(query.getPageNum(),query.getPageSize(),"dutieId desc");
        List<DutyDO> list = dutyMapper.listByQuery(query);
        if (ListUtil.isEmpty(list)){
            return new PageBean<>(ListUtil.emptyList(),query.getPageNum(),query.getPageSize(),0);
        }
        List<DutyDto> dtoList = new ArrayList<>(list.size());
        int count = dutyMapper.countByQuery(query);
        for (DutyDO dutyDO : list){
            DutyDto dto = new DutyDto();
            //职务主键
            dto.setDutieId(dutyDO.getDutieId());
            //分组id

            if (dutyDO.getGroupId()!=null){
                String groupName = dutyMapper.selectGroupName(dutyDO.getGroupId());
                if (groupName!=null) {
                    dto.setGroupName(groupName);
                    dto.setGroupId(dutyDO.getGroupId());
                }else {
                    dto.setGroupName("--");
                    dto.setGroupId(0);
                }
            }else {
                dto.setGroupName("--");
                dto.setGroupId(0);
            }
            //人数
            dto.setCountPeople(dutyMapper.countBydetiesId(dutyDO.getDutieId()));
            //职务名称
            if (dutyDO.getName()!=null){
                dto.setName(dutyDO.getName());
            }else {
                dto.setName("--");
            }
            //职务描述
            if (dutyDO.getRemarks()!=null){
                dto.setRemarks(dutyDO.getRemarks());
            }else {
                dto.setRemarks("暂无描述");
            }
            //部门信息
            String deptName = dutyMapper.selectDeptName(dutyDO.getDeptId());
            if (deptName!=null) {
                dto.setDeptName(deptName);
                dto.setDeptId(dutyDO.getDeptId());
            }else {
                dto.setDeptName("--");
                dto.setDeptId(0);
            }
            dto.setPinyin(dutyDO.getPinyin());
            dto.setAbbr(dutyDO.getAbbr());
            dtoList.add(dto);
        }
        return new PageBean<>(dtoList,query.getPageNum(),query.getPageSize(),count);
    }

    /**
     * 逻辑删除职务信息
     * @param dutyId
     * @return
     */
    @Override
    public int delete(Integer dutyId) {
        if (dutyId!=null){
            int i = dutyMapper.updateDety(dutyId);
            if (i<0){
                throw new AppException("删除职务失败",RequestCode.forbid_error);
            }
        }
        return 1;
    }

    @Override
    public int updateDuty(DutyForm form) {
        //非空校验
        if (form.getName()==null) {
            throw new AppException("职务名称不可为空", RequestCode.forbid_error);
        }
        if (form.getPinyin()==null) {
            throw new AppException("名称拼音不可为空",RequestCode.forbid_error);
        }
        if (form.getAbbr()==null) {
            throw new AppException("拼音首字母不可为空",RequestCode.forbid_error);
        }
//        if (form.getDeptId()==null){
//            throw new AppException("部门名称不可为空",RequestCode.forbid_error);
//        }

        DutyDO dutyDO = new DutyDO();
        dutyDO.setDutieId(form.getDutieId());
        dutyDO.setName(form.getName());
        dutyDO.setPinyin(form.getPinyin());
        dutyDO.setAbbr(form.getAbbr());
        dutyDO.setDeptId(form.getDeptId());
        dutyDO.setGroupId(form.getGroupId());
        dutyDO.setRemarks(form.getRemarks());
        if (form.getDutieId() != null){
            int version = dutyMapper.selectVersion(form.getDutieId());
            dutyDO.setVersion(version);
            dutyDO.setLastModifyTime(new Date());
            int i = dutyMapper.updateDuty(dutyDO);
            if (i<0){
                throw new AppException("修改职务失败",RequestCode.forbid_error);
            }
        }else {
            Integer dutyName = dutyMapper.selectDutyName(form.getName());
            if (dutyName>0){
                throw new AppException("职位名称不符合规定",RequestCode.forbid_error);
            }
            int i = dutyMapper.insertDuty(dutyDO);
            if (i<0){
                throw new AppException("新增职务失败",RequestCode.forbid_error);
            }
        }
        return 1;
    }

    @Override
    public List<DeptDTO> allDept(DeptEmpQuery query) {

        List<DeptDTO> deptDTOS = dutyMapper.allDept();
        return deptDTOS;
    }

    @Override
    public List<JSONObject> dutuies(String token) {
        return dutyMapper.dutuies();
    }

    /**
     * 查询子分组id
     *
     * @param parentId
     * @return
     */

    public List<Integer> listGroupId(Integer parentId) {
        List<GroupChildrenDO> list = dutyMapper.listGroupId(parentId);
        List<Integer> integers = new ArrayList<>(list.size());
        integers.add(parentId);
        for (GroupChildrenDO groupChildrenDO : list) {
            integers.add(groupChildrenDO.getGroupId());
        }
        return integers;
    }
}
